<?xml version="1.0" encoding="utf-8"?>
<component>
    <?component error="false" debug="false"?>
    <registration description="system" progid="component.template" version="1.00" clsid="{a912e98a-175c-423d-a8f7-7612ccf87020}" />
    <public>
        <property name="version" />
        <property name="directory" />
        <method name="render">
            <parameter name="data" />
        </method>
    </public>
    <implements type="ASP" id="ASP"/>
    <script language="JScript" runat="server">
    <![CDATA[
        var version = '1.00';
        var directory = 'templates/';
        
        var _content = '';
        
        function _load(file) {
            var _file = Server.MapPath(directory + (file || String(Request.ServerVariables('PATH_INFO')).replace('/', '')));
            if (_file) {
                var _content = '';
                var _fileSystemObject = Server.CreateObject('Scripting.FileSystemObject');
                if (_fileSystemObject.FileExists(_file)) {
                    var _stream = Server.CreateObject('ADODB.Stream');
                    _stream.Open();
                    _stream.Type = 1;
                    _stream.LoadFromFile(_file);
                    _stream.Position = 0;
                    _stream.Type = 2;
                    _stream.Charset = "UTF-8";
                    _content = _stream.ReadText();
                    _stream.Close();
                    delete(_stream);
                }
                return _content;
            }
            else {
                return '';
            }
        }
        
        function _parse(content) {
            var _content = content || '';
            
            var _includes = _content.match(/<!--\s*\#include\s+(file|virtual)\s*=\s*['"][^'"].*['"]\s*-->/ig);
            if (_includes != null) {
                var _key, _value = '';
                for (var _index = 0; _index < _includes.length; _index++) {
                    _key =     _includes[_index].toString();
                    _value = _parse(_load(_key.match(/=.*.asp/ig).toString().replace(/=\s*["|']/, '')));
                    _content = _content.replace(new RegExp(_key, 'ig'), _value);
                }
            }
            
            return _content;
        }
        
        function render(data) {
            var _data = data || {};
            var _content = _parse(_load());

            var _modules = [];
            
            var _matches = _content.match(/<!--.+?\{module:.{0,}?\}(.|\n)+?\{\/module:.{0,}?\}.+?-->/ig);
            for (var _index = 0; _matches && _index < _matches.length; _index++) {
                var _module = {};
                _module.value = String(_matches[_index]);
                
                _module.type = String(_module.value.match(/\{module:([^\s]*)/g)).replace(/\{module:/ig, '');
                
                _module.property = eval('(' + String(_module.value.match(/\{module:.{0,}?\}/g)).replace(new RegExp('module:' + _module.type, 'ig'), '').replace(/\{\s*/ig, '{').replace(/\'\s+/ig, '\', ').replace(/=/ig, ': ') + ')');
                
                _module.item = _module.value.replace(/(<!--.+?\{module:.{0,}?\}.+?-->|<!--.+?\{\/module:.{0,}?\}.+?-->)/ig, '');
                
                //_module.field = _module.item.match(/\{field:(.|\n)+?}/ig).join(',').replace(/(field:|\s)/ig, '').replace(/\{/g, '[').replace(/\}/g, ']').replace(/,/g, ', ');
                
                _module.data = [];
                
                _module.content = '';
                
                _modules[_index] = _module;
            }

            for (var _index = 0; _index < _modules.length; _index++) {
                switch (_modules[_index].type) {
                    case 'list':
                            _modules[_index].data = eval('_data.' + _modules[_index].property.data);
                            for (var _item in _modules[_index].data) {
                                _modules[_index].content += _modules[_index].item.replace(/{([^{}]*)}/g, function(_content, _match){
                                    if (!_match) return '';
                                    
                                    try {
                                        var _result = eval('with(_modules[_index].data[_item]){' + _content.replace(/(\s|\/)/g, '') + '}');
                                        return _result;
                                    }
                                    catch (_error) {
                                        return _content;
                                    }
                                });
                            }
                        break;
                }
                _content = _content.replace(_modules[_index].value, _modules[_index].content);
            }

            _content = _content.replace(/{([^{}]*)}/g, function(_content, _match){
                if (!_match) return '';
                
                try {
                    var _result = eval('with(_data){' + _content.replace(/(\s|\/)/g, '') + '}');
                    return _result;
                }
                catch (_error) {
                    return _content;
                }
            });
            return _content;
        }
        
    ]]>
    </script>
</component>